#include "HeartbeatDetectionRequestPanel.h"

#include "../../../../models/HeartbeatDetectionData.h"
#include "../../../../models/CommonData.h"
#include "../../../../controller/SignalBus.h"

#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>


HeartbeatDetectionRequestPanel::HeartbeatDetectionRequestPanel(QWidget *parent)
    : QWidget{parent}
{setupUi();}

void HeartbeatDetectionRequestPanel::initWidgets()
{
    const auto lbStatus = new QLabel(tr("状态"));
    const auto txtStatus = new QLineEdit();
    const auto statuLayout = new QHBoxLayout();
    statuLayout->addWidget(lbStatus);
    statuLayout->addWidget(txtStatus);
    valueList.insert("connectStatus",txtStatus);

    const auto btnSendStatus = new QPushButton(tr("发送状态"));
    const auto layout = new QVBoxLayout();
    layout->addLayout(statuLayout);
    layout->addWidget(btnSendStatus);

    setLayout(layout);

    connect(btnSendStatus,&QPushButton::clicked,this,&HeartbeatDetectionRequestPanel::execute);
}

void HeartbeatDetectionRequestPanel::setupUi()
{
    initWidgets();
}

void HeartbeatDetectionRequestPanel::execute()
{
    int randomNumber = QRandomGenerator::global()->bounded(10000,100000);
    QString dateTime = QDateTime::currentDateTime().toString("yyyyMMddhhmmssSSS");
    QString transIdStr = QString("%1%2").arg(dateTime).arg(randomNumber);
    std::string status = valueList["connectStatus"]->text().toStdString();

    BaseData<HeartbeatDetectionData>::execute(&HeartbeatDetectionData::setConnectStatus,status);
    BaseData<CommonData>::execute(&CommonData::setEventTime,dateTime.toStdString());
    BaseData<CommonData>::execute(&CommonData::setTransId,transIdStr.toStdString());

    emit SignalBus::instance()->sendHearbeatStatus();

}
